const nodemailer = require("nodemailer");
const Config = require('../config')

const transporter = nodemailer.createTransport({
    host: Config.nodemailer.host,
    secure: true,
    auth: {
        user: Config.nodemailer.user,
        pass: Config.nodemailer.pass,
    },
});

/**
 * 发送邮件
 * @param email
 * @param code
 * @param opear
 * @returns {Promise<void>}
 */
async function sendMail(email, code, opear = '注册验证码') {
    await transporter.sendMail({
        from: `"Chat ${opear} 👻" <xuecanb@163.com>`,
        to: email,
        subject: "Hello ✔",
        text: "Verify Code",
        html: `<b>${code}</b>`,
    });
}

/**
 * 生成6位验证码
 * @returns {string}
 */
function generateCode() {
    let code = parseInt(Math.random() * 1000000) + ''
    if (code.length < 6) {
        code = code + '0'
    }
    return code
}

module.exports = {
    generateCode,
    sendMail
}

